package cn.iocoder.yudao.module.erp.service.warehouseoutlist;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.erp.controller.admin.warehouseoutlist.vo.*;
import cn.iocoder.yudao.module.erp.dal.dataobject.warehouseoutlist.WarehouseOutListDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.erp.dal.mysql.warehouseoutlist.WarehouseOutListMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.*;

/**
 * ERP 出库单表（一二级库） Service 实现类
 *
 * @author songkai
 */
@Service
@Validated
public class WarehouseOutListServiceImpl implements WarehouseOutListService {

    @Resource
    private WarehouseOutListMapper warehouseOutListMapper;

    @Override
    public Long createWarehouseOutList(WarehouseOutListSaveReqVO createReqVO) {
        // 插入
        WarehouseOutListDO warehouseOutList = BeanUtils.toBean(createReqVO, WarehouseOutListDO.class);
        warehouseOutListMapper.insert(warehouseOutList);
        // 返回
        return warehouseOutList.getId();
    }

    @Override
    public void updateWarehouseOutList(WarehouseOutListSaveReqVO updateReqVO) {
        // 校验存在
        validateWarehouseOutListExists(updateReqVO.getId());
        // 更新
        WarehouseOutListDO updateObj = BeanUtils.toBean(updateReqVO, WarehouseOutListDO.class);
        warehouseOutListMapper.updateById(updateObj);
    }

    @Override
    public void deleteWarehouseOutList(Long id) {
        // 校验存在
        validateWarehouseOutListExists(id);
        // 删除
        warehouseOutListMapper.deleteById(id);
    }

    private void validateWarehouseOutListExists(Long id) {
        if (warehouseOutListMapper.selectById(id) == null) {
            throw exception(WAREHOUSE_OUT_LIST_NOT_EXISTS);
        }
    }

    @Override
    public WarehouseOutListDO getWarehouseOutList(Long id) {
        return warehouseOutListMapper.selectById(id);
    }

    @Override
    public PageResult<WarehouseOutListDO> getWarehouseOutListPage(WarehouseOutListPageReqVO pageReqVO) {
        return warehouseOutListMapper.selectPage(pageReqVO);
    }

}